package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootScinfo;
import org.footballmanager.pandafootball.system.form.impl.FootScinfoSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootScinfoMapper;
import org.footballmanager.pandafootball.system.service.FootScinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

@Slf4j
@Service
public class FootScinfoServiceImpl extends ServiceImpl<FootScinfo> implements FootScinfoService {
    private static final String USERID = Constants.USERID;
    @Autowired
    private FootScinfoMapper footScinfoMapper;

    /**
     * 是否收藏
     *
     * @param userId 用户主键
     * @param qyId   球员主键
     * @return 是否收藏
     */
    @Override
    public boolean isSc(Long userId, Long qyId) {
        Example example = new Example(FootScinfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("qyId", qyId);
        criteria.andEqualTo(USERID, userId);
        criteria.andEqualTo("delFlag", 0);
        FootScinfo fs = this.findByExample(example);
        return fs != null;
    }

    /**
     * 根据用户主键查找收藏信息
     *
     * @param userId 用户主键
     * @return 收藏列表
     */
    @Override
    public List<FootScinfo> findCollectionByUserId(Long userId) {
        Example example = new Example(FootScinfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(USERID, userId);
        criteria.andEqualTo("delFlag", 0);
        return this.findall(example);
    }

    /**
     * 根据用户主键和球员主键查找收藏记录
     *
     * @param userId 用户主键
     * @param qyId   球员主键
     * @return 收藏记录
     */
    @Override
    public FootScinfo findByUserIdAndQyid(Long userId, Long qyId) {
        Example example = new Example(FootScinfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(USERID, userId);
        criteria.andEqualTo("qyId", qyId);
        criteria.andEqualTo("delFlag", 0);
        return this.findByExample(example);
    }

    /**
     * 根据球员主键获取收藏信息
     *
     * @param qdId 球员主键
     * @return 收藏列表
     */
    @Override
    public List<FootScinfo> findByQyid(Long qyId) {
        Example example = new Example(FootScinfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("qyId", qyId);
        criteria.andEqualTo("delFlag", 0);
        return this.findall(example);
    }

    /**
     * 翻页方法
     *
     * @param footScinfoSearchForm 条件
     * @return 列表
     */
    @Override
    public List<FootScinfo> findall(FootScinfoSearchForm footScinfoSearchForm) {
        List<FootScinfo> list = footScinfoMapper.findall(footScinfoSearchForm);
        if (list != null && !list.isEmpty()) {
            for (FootScinfo footScinfo : list) {
                Long qyId = footScinfo.getQyId();
                footScinfo.setSswj(footScinfoMapper.findByQyId(qyId));
            }
        }
        return list;
    }

    /**
     * 根据主键获取记录
     *
     * @param id 主键
     * @return 记录
     */
    @Override
    public FootScinfo get(Serializable id) {
        return footScinfoMapper.findById(id);
    }

    /**
     * 查询
     *
     * @param entityname           实体类名
     * @param map                  参数
     * @param footScinfoSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootScinfoSearchForm footScinfoSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footScinfoSearchForm);
        Page<FootScinfo> page = PageHelper.startPage(footScinfoSearchForm.getPageIndex(), footScinfoSearchForm.getPageSize());
        List<FootScinfo> list = this.findall(footScinfoSearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 根据用户主键删除对应记录
     *
     * @param userId  用户主键
     * @param request 请求
     */
    @Override
    public void deleteByUserId(Long userId, HttpServletRequest request) {
        //删除收藏
        List<FootScinfo> scList = this.findCollectionByUserId(userId);
        if (scList != null && !scList.isEmpty()) {
            for (FootScinfo fs : scList) {
                this.delete(fs.getId(), request);
            }
        }
    }
}
